package com.atguigu.myclock;

class Conut {
    int number = 0;

    public synchronized void add() throws InterruptedException {
       // if (number!=0){
        while (number!=0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"===="+(++number));
        this.notify();
    }

    public synchronized void decre() throws InterruptedException {
       // if (number==0){
        while (number==0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"===="+(--number));
        this.notify();
    }
}

public class ThreadedCommunication {
    public static void main(String[] args) {
        Conut conut = new Conut();
        new Thread(()-> {
            for (int i = 0; i < 50; i++) {
                try {
                    conut.add();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"A").start();

        new Thread(()-> {
            for (int i = 0; i < 50; i++) {
                try {
                    conut.decre();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"B").start();

        new Thread(()-> {
            for (int i = 0; i < 50; i++) {
                try {
                    conut.add();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"C").start();

        new Thread(()-> {
            for (int i = 0; i < 50; i++) {
                try {
                    conut.decre();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"D").start();
    }
    }


